<?php


namespace app\admin\controller;

use think\Db;
use think\Validate;

/**
 * 特色
 * Class Region
 * @package app\admin\controller
 */
class Item extends Base
{

    /**
     * 特色列表
     */
    public function item_list()
    {
        $db = Db::name('item')->select();
        foreach ($db as $key => $value) {
            $db[$key]['urls'] = to_all_url($value['urls']);
        }
        $this->returnMsg('1', '', $db);
    }

    /**
     * 特色详情
     */
    public function item_msg()
    {
        $id = input('id', 0);
        $db = Db::name('item')->where(['id' => $id])->find();
        if ($db) {
            $db['urls'] = to_all_url($db['urls']);
        }
        $this->returnMsg('1', '', $db);
    }

    /**
     * 添加特色
     */
    public function add_item()
    {
        $add_data = I('.');
        unset($add_data['token']);
        $rule = [
            'names' => 'item_name|length:1,50',
            'urls' => 'require|length:1,200',
        ];

        $msg = [
            'names.require' => '请输入特色',
            'names.length' => '请输入1-50字的特色',
            'urls.require' => '请上传特色图标',
            'urls.length' => '特色图标格式不对'
        ];

        $validate = new Validate($rule, $msg);
        $result = $validate->check($add_data);
        if (!$result) {
            $this->returnMsg(-1, $validate->getError());
        }

        $names = $add_data['item_name'];
        $have_db = Db::name('item')->where(['item_name'=>$names])->find();
        if (!empty($have_db)) {
            $this->returnMsg(-1, '已有相同的特色', []);
        }

        $db = Db::name('item')->insert($add_data);
        $this->returnMsg(1, '添加成功', $db);
    }

    /**
     * 编辑城市
     */
    public function edit_item()
    {
        $add_data = I('.');
        unset($add_data['token']);

        $rule = [
            'id' => 'require|number',
            'names' => 'item_name|length:1,50',
            'urls' => 'require|length:1,200',
        ];

        $msg = [
            'id.require' => '请选择特色',
            'id.number' => '请选择特色编号',
            'names.require' => '请输入特色',
            'names.length' => '请输入1-50字的特色',
            'urls.require' => '请上传特色图标',
            'urls.length' => '特色图标格式不对'
        ];

        $validate = new Validate($rule, $msg);
        $result = $validate->check($add_data);
        if (!$result) {
            $this->returnMsg(-1, $validate->getError());
        }

        $iid = $add_data['id'];

        $item_db = Db::name('item')->where(['id' => $iid])->find();
        if (!$item_db) {
            $this->returnMsg(-1, '没有找到特色数据', []);
        }

        $names = $add_data['item_name'];
        $have_db = Db::name('item')->where(['item_name' => $names, 'id' => ['neq', $iid]])->find();
        if (!empty($have_db)) {
            $this->returnMsg(-1, '已有相同的特色名称', []);
        }

        $db = Db::name('item')->where(['id' => $iid])->update($add_data);
        $this->returnMsg(1, '修改成功', $db);
    }

    /**
     * 删除城市
     */
    public function delete_item()
    {
        $iid = input('id');
        $store_item = Db::name('store_item')->where(['item_id' => $iid])->find();
        if ($store_item) {
            $this->returnMsg(-1, '已有仓库使用该特色');
        }

        Db::name('item')->where(['id' => $iid])->delete();
        $this->returnMsg(1, '删除成功');
    }

}